require 'rubygems'
require 'ferret'
require 'time'
require 'progressbar'

include Ferret

namespace :index do
  
  desc "Create the index for mefial profile"
  task :create => :environment do
    #index_base_path = ENV['INDEX_PATH'] || "#{Rails.public_path}/data/indexs"
    index_path =  ENV['INDEX_PATH'] || "#{Rails.public_path}/data/indexs/medial_profile"
    #create = ENV['CREATE'] || false
    
    optimize_threshold = ENV['OPTIMIZE_THRESHOLD'].to_i || 100000
    conditions = ENV['CONDITIONS'] || "true"
    
    args_create = {
    :path => index_path,
    :create => true,
    :max_buffer_memory  => 0x700000,
    :max_buffered_docs => 20000,
    :max_merge_docs => 100000
    }
    
    record_to_index = User.count_by_sql "SELECT count(*) FROM #{MedialProfile.table_name} WHERE #{conditions}"
    
    pbar = ProgressBar.new "create idx", 100
    last_progress_value = 0
    progress_value = last_progress_value
    record_indexed = 0
    
    puts "Start creating index in #{index_path}"
    idx = create_index args_create
    
    MedialProfile.find_each(:conditions => conditions) { |m|
      
      add_medial_profile_to_index(idx ,m)
      
      record_indexed += 1
      progress_value = sprintf("%d", record_indexed.to_f / record_to_index * 100.0).to_i
      if  progress_value != last_progress_value then
        last_progress_value = progress_value
        pbar.set progress_value
      end
    }        
    
    puts pbar.finish
    
    #    index_id = 0
    
    
    
    puts "Start optimize index #{DateTime.now}"
    idx.flush
    idx.optimize
    idx.close
    puts "Finish optimize index #{DateTime.now}"
  end
  
  desc "Update the index entrys"
  task :update => :environment do
    #  puts "Prize: #{pick(Services).name}"
  end
  
  def add_medial_profile_to_index(idx, medial_profile)
    doc = Ferret::Document.new
    
    doc[:id] = medial_profile.id
    doc[:user_id] = medial_profile.user_id
    doc[:full_name] = medial_profile.full_name
    doc[:first_name] = medial_profile.first_name
    doc[:last_name] = medial_profile.last_name
    doc[:age] = medial_profile.age
    doc[:gender] = medial_profile.gender
    doc[:locale_language] = medial_profile.locale_language
    doc[:rel_status] = medial_profile.rel_status
    doc[:email] = medial_profile.email
    doc[:occupation] = medial_profile.occupation
    doc[:role] = medial_profile.role
    doc[:locality] = medial_profile.locality
    doc[:state] = medial_profile.state
    doc[:country] = medial_profile.country
    doc[:locale_country] = medial_profile.locale_country
    doc[:full_hometown] = medial_profile.full_hometown
    doc[:full_locations] = medial_profile.full_locations
    doc[:music] = medial_profile.music
    doc[:movies] = medial_profile.movies
    doc[:interests] = medial_profile.interests
    doc[:books] = medial_profile.books
    doc[:service_count] = medial_profile.service_count
    doc[:last_update] = medial_profile.last_update
    
    idx.add_document doc
  end
  
  def create_index(args) 
    idx = Index::Index.new args
    
    field_infos = idx.field_infos
    field_infos.add_field :id, :index => :no, :term_vector => :no
    field_infos.add_field :user_id, :index => :no, :term_vector => :no 
    field_infos.add_field :full_name
    field_infos.add_field :first_name 
    field_infos.add_field :last_name
    field_infos.add_field :locale_language 
    field_infos.add_field :rel_status
    field_infos.add_field :email
    field_infos.add_field :occupation
    field_infos.add_field :role 
    field_infos.add_field :locality
    field_infos.add_field :state 
    field_infos.add_field :country
    field_infos.add_field :locale_country
    field_infos.add_field :full_hometown
    field_infos.add_field :full_locations
    field_infos.add_field :music
    field_infos.add_field :movies
    field_infos.add_field :interests 
    field_infos.add_field :books
    field_infos.add_field :service_count
    field_infos.add_field :last_update, :index => :no, :term_vector => :no
    idx
  end
  
  #  desc "Pick a random prize and winner"
  #  task :all => [:prize, :winner]
  
  #  def pick(model_class)
  #    model_class.find(:first, :order => 'RAND()')
  #  end
end